﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CoyoEden;
using System.Collections.Generic;
using System.Collections.Specialized;
using CoyoEden.Models;

public partial class widgets_PostsSlider_widget :WidgetBase
{
	protected void Page_Load(object sender, EventArgs e)
	{
		//css and js
		if (!Page.IsPostBack)
		{
			string jsPath = string.Format("{0}assets/js/widgets/{1}.js", Utils.RelativeWebRoot, this.Name);
			AddWidgetScript(jsPath);
		}
	}

	#region base implementations
	public override string Name
	{
		get { return "PostsSlider"; }
	}

	public override bool IsEditable
	{
		get { return true; }
	}

	public override void LoadWidget()
	{
		LoadSettings();
		BuildLayout();
	}
	#endregion

	#region member variables
	private List<Post> Posts;
	private Category TheCategory;
	#endregion

	#region helper methods
	private void LoadSettings() {
		StringDictionary settings = GetSettings();
		int cid =-1;
		int postnum = 0;
		if (settings.ContainsKey("clist")) {
			cid =Convert.ToInt32(settings["clist"]);
			TheCategory = Category.GetCategory(cid);
		}
		if (settings.ContainsKey("postnum")) {
			postnum = Convert.ToInt16(settings["postnum"]);
		}
		if (cid<0) return;
		Posts = Post.GetPostsByCategory(cid).OrderBy(x=>x.DateCreated).Take(postnum).ToList();
	}

	private void BuildLayout() {
		if (Posts == null || Posts.Count == 0) {
			phContent.Controls.Add(new LiteralControl("<h3>没有任何可以显示的内容</h3>"));
			return;
		}
		//title part
		var title = this.Title;
		HtmlAnchor anc = new HtmlAnchor();
		anc.HRef = Utils.RelativeWebRoot + "category/" + TheCategory.Id + PortalSettings.Instance.FileExtension;
		anc.Title = "";
		anc.InnerHtml = title;
		this.phTitle.Controls.Add(anc);

		//content & function part
		int i = 0;
		Posts.ForEach(p => {
			var div = BuildPostContent(p);
			phContent.Controls.Add(div);

			div = BuildFunctionButton(++i);
			phButtons.Controls.Add(div);
		});


	}
	private HtmlGenericControl BuildPostContent(Post post) {
		HtmlGenericControl retVal = new HtmlGenericControl("div");
		retVal.Attributes["class"] = "entry";

		//post icon 
		string icon = Utils.AbsoluteWebRoot+"assets/img/defaultPostIcon.jpg";
		Attachment attachedIcon = post.Attachments.Items.Find(item=>item.IsImage==true);
		if (!attachedIcon.Equals(null)) {
			icon = attachedIcon.Url+"&suffix=300x180";
		}
		HtmlGenericControl postIcon = new HtmlGenericControl("div");
		postIcon.Attributes["class"] = "icon";
		postIcon.InnerHtml = string.Format("<div class=\"iconC\" onclick=\"location.href='{0}'\"><img alt=\"\" src=\"{1}\"/></div>",post.AbsoluteLink.ToString(), icon);

		//post main
		HtmlGenericControl postMain = new HtmlGenericControl("div");
		postMain.Attributes["class"] = "article";
		//post marin-title
		HtmlGenericControl postTitle = new HtmlGenericControl("div");
		postTitle.Attributes["class"] = "title";
		postTitle.InnerHtml = string.Format("<span class=\"size13\"><a href=\"{0}\">{1}</a></span>",post.AbsoluteLink.ToString(),post.Title);

		postMain.Controls.Add(postTitle);

		//post main-desc

		//assemble the post
		retVal.Controls.Add(postIcon);
		retVal.Controls.Add(postMain);

		return retVal;

	}
	private HtmlGenericControl BuildFunctionButton(int index) {
		HtmlGenericControl retVal = new HtmlGenericControl("div");
		retVal.Attributes["class"] = "btn";
		retVal.InnerHtml = string.Format("<a class=\"\" href=\"javascript:void(0)\">{0}</a>",index);
		return retVal;
	}
	#endregion
}
